//
//  CLTabbarVC.m
//  ZYSliderViewController
//
//  Created by yoyo on 2021/3/8.
//  Copyright © 2021 CL. All rights reserved.
//

#import "CLTabbarVC.h"

@interface CLTabbarVC ()
@property (nonatomic, strong) UIView *backView;//承载所有的子视图
@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic, strong) UIButton *iconBtn;
@property (nonatomic, strong) UIButton *iconTextBtn;


@property (nonatomic, strong) UIButton *widgetBtn;
@property (nonatomic, strong) UIButton *widgeTextBtn;

@property (nonatomic, strong) UIButton *diyBtn;
@property (nonatomic, strong) UIButton *diyTextBtn;

@end

@implementation CLTabbarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTabBarTranslucent];
    [self creatSubView];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (@available(iOS 11.0, *))
    {
        UIWindow *window = UIApplication.sharedApplication.keyWindow;
        CGFloat topPadding = window.safeAreaInsets.top;
        CGFloat bottomPadding = window.safeAreaInsets.bottom;
        CGFloat leftPadding = window.safeAreaInsets.left;
        CGFloat rightPadding = window.safeAreaInsets.right;
        CGFloat height = self.view.safeAreaLayoutGuide.layoutFrame.size.height;
        NSLog(@"topPadding:%f bottomPadding:%f height:%f, viewHeight:%f leftPadding:%f, rightPadding:%f", topPadding, bottomPadding, height, self.view.frame.size.height, leftPadding, rightPadding);
    }
}

- (void)configTabBarTranslucent
{
    [[UITabBar appearance] setShadowImage:[self createImageWithColor:[UIColor clearColor]]];// 覆盖原生Tabbar的上横线
    [[UITabBar appearance] setBackgroundImage:[self createImageWithColor:[UIColor clearColor]]];// 背景图片为透明色
     self.tabBar.backgroundColor = [UIColor clearColor];
    [UITabBar appearance].translucent = YES;

}

- (UIImage *)createImageWithColor:(UIColor *)color
{
  CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  UIGraphicsBeginImageContext(rect.size);
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetFillColorWithColor(context, [color CGColor]);
  CGContextFillRect(context, rect);
  UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
  return theImage;
}

- (void)creatSubView
{
    [self.view addSubview:self.backView];
    @weakify(self);
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(123);
    }];
    
    self.backImageView = [[UIImageView alloc] init];
    self.backImageView.image = [UIImage imageNamed:@"home_bot_img"];
    [self.backView addSubview:self.backImageView];
    [self.backImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.left.bottom.right.equalTo(self.backView);
    }];
    
    // Widget btn
    [self.backView addSubview:self.widgetBtn];
    [self.widgetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.equalTo(self.backView.mas_top).offset(-25);
        make.width.height.mas_equalTo(68);
        make.centerX.equalTo(self.backView);
    }];
    
    [self.backView addSubview:self.widgeTextBtn];
    [self.widgeTextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.equalTo(self.widgetBtn.mas_bottom).offset(10);
        make.height.mas_equalTo(20);
        make.centerX.equalTo(self.widgetBtn);
    }];

    // Icon Btn
    [self.backView addSubview:self.iconBtn];
    [self.iconBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.equalTo(self.widgetBtn.mas_bottom).offset(-13);
        make.width.mas_equalTo(27);
        make.height.mas_equalTo(24);
        make.centerX.mas_equalTo(self.view.frame.size.width * -0.25);
    }];
    
    [self.backView addSubview:self.iconTextBtn];
    [self.iconTextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.equalTo(self.iconBtn.mas_bottom).offset(0);
        make.height.mas_equalTo(20);
        make.centerX.equalTo(self.iconBtn);
    }];
    
    
    // DIY btn
    [self.backView addSubview:self.diyBtn];
    [self.diyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.equalTo(self.iconBtn.mas_top);
        make.width.mas_equalTo(26);
        make.height.mas_equalTo(26);
        make.centerX.mas_equalTo(self.view.frame.size.width * 0.25);
    }];
    
    [self.backView addSubview:self.diyTextBtn];
    [self.diyTextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.equalTo(self.diyBtn.mas_bottom).offset(0);
        make.height.mas_equalTo(20);
        make.centerX.equalTo(self.diyBtn);
    }];
}

- (UIView *)backView
{
    if (!_backView) {
        _backView = [[UIView alloc] init];
        _backView.backgroundColor = [UIColor clearColor];
    }
    return _backView;
}

- (UIButton *)widgetBtn
{
    if (!_widgetBtn) {
        _widgetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        SEL action = @selector(clickWidget:);
        [_widgetBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        [_widgetBtn setBackgroundImage:[UIImage imageNamed:@"home_wg"] forState:UIControlStateNormal];
        
    }
    return _widgetBtn;
}

- (UIButton *)widgeTextBtn
{
    if (!_widgeTextBtn) {
        _widgeTextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        SEL action = @selector(clickWidget:);
        [_widgeTextBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        _widgeTextBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight: UIFontWeightRegular];
        [_widgeTextBtn setTitle:@"Widget" forState:UIControlStateNormal];
    }
    return _widgeTextBtn;
}


- (UIButton *)iconBtn
{
    if (!_iconBtn) {
        _iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        SEL action = @selector(clickIcon:);
        [_iconBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        [_iconBtn setBackgroundImage:[UIImage imageNamed:@"home_diamond"] forState:UIControlStateNormal];
        
    }
    return _iconBtn;
}

- (UIButton *)iconTextBtn
{
    if (!_iconTextBtn) {
        _iconTextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        SEL action = @selector(clickIcon:);
        [_iconTextBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        _iconTextBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight: UIFontWeightRegular];
        [_iconTextBtn setTitle:@"Icon" forState:UIControlStateNormal];
    }
    return _iconTextBtn;
}

- (UIButton *)diyBtn
{
    if (!_diyBtn) {
        _diyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        SEL action = @selector(clickDiy:);
        [_diyBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        [_diyBtn setBackgroundImage:[UIImage imageNamed:@"home_diy"] forState:UIControlStateNormal];
        
    }
    return _diyBtn;
}

- (UIButton *)diyTextBtn
{
    if (!_diyTextBtn) {
        _diyTextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _diyTextBtn.titleLabel.font = [UIFont systemFontOfSize:14 weight: UIFontWeightRegular];
        SEL action = @selector(clickDiy:);
        [_diyTextBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
        [_diyTextBtn setTitle:@"DIY" forState:UIControlStateNormal];
    }
    return _diyTextBtn;
}
#pragma mark - event touch

- (void)clickIcon:(UIButton *)sender
{
    self.selectedIndex = 0;

}

- (void)clickWidget:(UIButton *)sender
{
    
    self.selectedIndex = 1;
}

- (void)clickDiy:(UIButton *)sender
{
    self.selectedIndex = 2;

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
